from baiduapi import BaiduPCS

API_KEY = '你的API Key'
SECRET_KEY = '你的Secret Key'

# 初始化 BaiduPCS
pcs = BaiduPCS(API_KEY, SECRET_KEY)

# 获取访问令牌（Access Token）
token = pcs.access_token()

# 设置文件上传的目标路径
remote_path = '/apps/your_remote_folder/large_file.txt'

# 分片上传
local_path = 'path/to/large_file.txt'  # 本地大文件路径
result = pcs.init_upload_parts(local_path, remote_path, token)

# 获取上传ID和offset
upload_id = result['upload_id']
offset = result['size']

# 设置分片大小（默认为4MB）
part_size = 4 * 1024 * 1024

# 逐个分片上传
with open(local_path, 'rb') as f:
    f.seek(offset)
    chunk = f.read(part_size)
    while chunk:
        response = pcs.upload_part(chunk, upload_id, offset, token)
        offset += len(chunk)
        chunk = f.read(part_size)

# 完成分片上传
response = pcs.complete_upload_parts(upload_id, remote_path, token)

# 打印上传结果
print(response)